<?php namespace Admin\Controller;

class GoodslistController extends CommonController{
	private $model;
	private static $sta_gid;
	public function __init(){
		//调用CommonController里面的构造函数
		parent::__init();
		//实例化
	  	$this->model = new \Admin\Model\Goods; 
	}
	public function index(){
	 	
	    $gid = Q('gid',0,'intval');
		self::$sta_gid = $gid;
		$tid = $this->model->where("gid={$gid}")->pluck('type_tid');
		$model = new \Admin\Model\Typeattr;
		$typeattrdata = $model->where("type_tid={$tid} and taclass=1")->get();
		View::with('typeattrdata',$typeattrdata);
		
		$model2 = new \Admin\Model\Goodslist;
		$goodslistdata = $model2->where("goods_gid={$gid}")->get();
		View::with('goodslistdata',$goodslistdata);
		$userattr = array();
		foreach ($goodslistdata as $key => $value) {
			$userattr[$value['glid']] = explode(',',$value['glattrid']);
		}
		View::with('userattr',$userattr);
		
		$model3 = new \Admin\Model\Goodsattr;
		$goodsattrdata = $model3->where("goods_gid={$gid} and gaadd != 'em'")->get();
		View::with('goodsattrdata',$goodsattrdata);
		

//		接收数据
		if(IS_POST){
			if($model2->store()){
				
				View::success('添加成功',U('index',array('gid' =>$gid)));
			}  
			View::error($model2->getError());
		}
		
		View::make();
	}
	
	//删除
	public function del(){
		$model = new \Admin\Model\Goodslist;
	    $glid = Q('get.glid',0,'intval');
		$gid = Q('get.id',0,'intval');
	    $model->where("glid=$glid")->delete();
		View::success('删除成功',U('index',array('gid' =>$gid)));
	}
//	编辑
 
	public function edit(){
		$glid = Q('post.id',0,'intval');
		$data = Db::table('Goodslist')->where("glid=$glid")->get();
		if(IS_POST){
		    $data->where("glid=$glid")->delete();
			if($data->store()){
				View::success('编辑成功',U('index',array('gid' =>$gid)));
			}  
			View::error($data->getError());
		}
		

	}
	
}
	